<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
	<title>映射地理形状 | Elasticsearch: 权威指南 | Elastic</title>
    <!-- Give IE8 a fighting chance -->
    <!--[if lt IE 9]>
    <script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
    <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
    <![endif]-->
	<link rel="stylesheet" type="text/css" href="../static/styles.css" />
</head>
<body>
<div class="main-container">
    <section id="content">
        
        <div class="content-wrapper">
            <section id="guide" lang="zh_cn">
                <div class="container">
                    <div class="row">
                        <div class="col-xs-12 col-sm-8 col-md-8 guide-section">
                            <div style="color:gray; word-break: break-all; font-size:12px;">原文地址: <a href="https://www.elastic.co/guide/cn/elasticsearch/guide/current/mapping-geo-shapes.html" rel="nofollow">https://www.elastic.co/guide/cn/elasticsearch/guide/current/mapping-geo-shapes.html</a>, 版权归 www.elastic.co 所有<br/>
                            英文版地址: <a href="https://www.elastic.co/guide/en/elasticsearch/guide/current/mapping-geo-shapes.html" rel="nofollow">https://www.elastic.co/guide/en/elasticsearch/guide/current/mapping-geo-shapes.html</a>
                            </div>
                        <!-- start body -->
                  <div class="page_header">
<b>请注意:</b><br>本书基于 Elasticsearch 2.x 版本，有些内容可能已经过时。
</div>
<div id="content">
<div class="breadcrumbs">
<span class="breadcrumb-link"><a href="index.html">Elasticsearch: 权威指南</a></span>
»
<span class="breadcrumb-link"><a href="geoloc.html">地理位置</a></span>
»
<span class="breadcrumb-link"><a href="geo-shapes.html">地理形状</a></span>
»
<span class="breadcrumb-node">映射地理形状</span>
</div>
<div class="navheader">
<span class="prev">
<a href="geo-shapes.html">« 地理形状</a>
</span>
<span class="next">
<a href="indexing-geo-shapes.html">索引地理形状 »</a>
</span>
</div>
<div class="section">
<div class="titlepage"><div><div>
<h2 class="title">
<a id="mapping-geo-shapes"></a>映射地理形状 (Mapping Geo Shapes)<a class="edit_me edit_me_private" rel="nofollow" title="Editing on GitHub is available to Elastic" href="https://github.com/elasticsearch-cn/elasticsearch-definitive-guide/edit/cn/340_Geoshapes/72_Mapping_geo_shapes.asciidoc">edit</a>
</h2>
</div></div></div>
<p>与 <code class="literal">geo_point</code> 类型的字段相似， 地理形状(geo-shapes)也必须在使用前显式定义映射：</p>
<div class="pre_wrapper lang-json">
<pre class="programlisting prettyprint lang-json">PUT /attractions
{
  "mappings": {
    "landmark": {
      "properties": {
        "name": {
          "type": "string"
        },
        "location": {
          "type": "geo_shape"
        }
      }
    }
  }
}</pre>
</div>
<p>你需要考虑修改两个非常重要的设置： <code class="literal">精度(precision)</code> 和 <code class="literal">距离误差(distance_error_pct)</code> 。</p>
<div class="section">
<div class="titlepage"><div><div>
<h3 class="title">
<a id="_精度"></a>精度 (precision)<a class="edit_me edit_me_private" rel="nofollow" title="Editing on GitHub is available to Elastic" href="https://github.com/elasticsearch-cn/elasticsearch-definitive-guide/edit/cn/340_Geoshapes/72_Mapping_geo_shapes.asciidoc">edit</a>
</h3>
</div></div></div>
<p><code class="literal">精度(precision)</code> 参数用来控制生成的 geohash 的最大长度。默认精度为 <code class="literal">9</code> ，等同于尺寸在 5m x 5m 的<a class="xref" href="geohashes.html" title="Geohashes">geohash</a> 。这个精度可能比你需要的精确得多。</p>
<p>精度越低，需要索引的词项(term)就越少，检索时也会更快。当然，精度越低，地理形状的准确性就越差。你需要考虑自己的地理形状所需要的精度 —— 即使减少1-2个等级都可以大大的节省成本。</p>
<p>你可以使用距离来指定精度，比如 <code class="literal">50m</code> 或 <code class="literal">2km</code>，不过这些距离最终也会转换成<a class="xref" href="geohashes.html" title="Geohashes"><em>Geohashes</em></a>中描述的等级。</p>
</div>

<div class="section">
<div class="titlepage"><div><div>
<h3 class="title">
<a id="_距离误差"></a>距离误差 (distance_error_pct)<a class="edit_me edit_me_private" rel="nofollow" title="Editing on GitHub is available to Elastic" href="https://github.com/elasticsearch-cn/elasticsearch-definitive-guide/edit/cn/340_Geoshapes/72_Mapping_geo_shapes.asciidoc">edit</a>
</h3>
</div></div></div>
<p>当索引一个多边形时，中间连续区域很简单的用一个短 geohash <span style="color:#666;">字符串</span> 来表示。麻烦的是边缘部分，这些地方需要使用更精细的 geohash 才能表示。</p>
<p>当你在索引一个小地标时，你会希望它的边界比较精确。让这些纪念碑一个叠着一个可不好。当索引整个国家时，你就不需要这么高的精度了。误差个50米左右也不可能引发战争。</p>
<p><code class="literal">distance_error_pct</code> 指定地理形状可以接受的最大错误率。默认值是 <code class="literal">0.025</code> ， 即 2.5% 。也就是说，大的地理形状（比如国家）相比小的地理形状（比如纪念碑）来说，容许更加模糊的边界。</p>
<p>默认值<code class="literal">0.025</code> 是一个不错的起点。不过，如果我们容许更大的错误率，对应地理形状需要索引的term就越少。</p>
</div>

</div>
<div class="navfooter">
<span class="prev">
<a href="geo-shapes.html">« 地理形状</a>
</span>
<span class="next">
<a href="indexing-geo-shapes.html">索引地理形状 »</a>
</span>
</div>
</div>

                  <!-- end body -->
                        </div>
                        <div class="col-xs-12 col-sm-4 col-md-4" id="right_col">
                        
                        </div>
                    </div>
                </div>
            </section>
        </div>
    </section>
</div>
<script src="../static/cn.js"></script>
</body>
</html>